package com.kc.sjwh.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.AppDao;
import com.kc.sjwh.dao.RoleDao;
import com.kc.sjwh.dao.RoleUserDao;
import com.kc.sjwh.dao.UserDao;
import com.kc.sjwh.entity.po.App;
import com.kc.sjwh.entity.po.Role;
import com.kc.sjwh.entity.po.RoleUser;
import com.kc.sjwh.entity.po.User;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.other.Tool;

/**
 * 用户service
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Service
public class UserService {
	static Logger logger = Logger.getLogger(UserService.class);
	@Autowired
	UserDao userDao;
	@Autowired
	RoleUserDao roleUserDao;
	@Autowired
	RoleDao roleDao;
	@Autowired
	AppDao appDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<User> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("cjsj desc, id");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("ssdwdm like '?%'".replace("?", Tool.rightTrim(v, "00")));
		}
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("mc like ? or zh like ?".replace("?", "'%"+v+"%'"));
		}
		
		return userDao.getPage("get", q);
	}

	public void add(User one) {
		User exist = userDao.getOne(new QueryParam().put("zh", one.getZh()));
		if(exist != null) {
			throw new IllegalArgumentException("账号 [ "+one.getZh()+" ] 已存在, 已被 [ "+one.getMc()+" ] 注册");
		}
		
		one.setId(Tool.getUuid());
		one.setCjsj(new Date());
		
		userDao.add(one);
	}

	@Transactional
	public void update(User one, String jsids) {
		//清空角色关联
		roleUserDao.deleteByYhid(one.getId());
		//添加角色关联s
		for(String jsid : jsids.split(",")) {
			roleUserDao.add(jsid, one.getId());
		}
		
		one.setGxsj(new Date());
		userDao.update(one, "id");
	}

	public void delete(String id) {
		//级联删除
		roleUserDao.deleteByYhid(id);
		
		int n = userDao.delete(id);
		if(n == 0) {
			throw new IllegalArgumentException("要删除的记录不存在");
		}
	}

	public Map load4Update(String id) {
		Map map = new HashMap();
		
		//加载资源s
		List<RoleUser> list = roleUserDao.getList(new QueryParam().put("yhid", id));
		List<String> jsids = new ArrayList<String>();
		for(RoleUser ru : list) {
			jsids.add(ru.getJsid());
		}
		map.put("jsids", StringUtils.join(jsids, ","));
		
		map.put("one", userDao.getOne(new QueryParam().put("id", id)));
		
		List<App> apps = appDao.getList(new QueryParam());
		List<Role> roles = roleDao.getList(new QueryParam());
		List<Map> l = new ArrayList<Map>();
		for(App a : apps) {
			Map m = new HashMap();
			m.put("id", a.getId());
			m.put("name", a.getMc());
			m.put("nocheck", true);
			l.add(m);
		}
		for(Role r : roles) {
			Map m = new HashMap();
			m.put("id", r.getId());
			m.put("name", r.getMc());
			m.put("pid", r.getYyid());
			l.add(m);
		}
		map.put("roleList", l);
		
		return map;
	}
	
}
